package com.woniuxy.microsysserver.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.woniuxy.bean.BillBean;
import com.woniuxy.bean.MemberEntity;
import com.woniuxy.bean.ResultBean;
import com.woniuxy.bean.current.PageInfos;
import com.woniuxy.bean.regularbean.MemberAccountOutBean;
import com.woniuxy.microsysserver.feign.CurrentFoundFeign;
import com.woniuxy.microsysserver.feign.FixedTimeFeign;
import com.woniuxy.microsysserver.feign.FoundFeign;
import com.woniuxy.microsysserver.feign.GoldFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Auther: wuson  <br/>
 * Date: 2021/4/12:20:19  <br/>
 * Description:收益
 */
@RefreshScope
@Api("门户端查看收益")
@Slf4j
@RequestMapping(value = "/sys/profit", produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
public class ProfitController {
    @Autowired(required = false)
    private FixedTimeFeign fixedTimeFeign;
    @Autowired(required = false)
    private CurrentFoundFeign currentFoundFeign;
    @Autowired(required = false)
    private GoldFeign goldFeign;
    @Autowired(required = false)
    private FoundFeign foundFeign;

    @ApiOperation("定期收益")
    @PostMapping(value = "/fixedtime", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getFixedtime (@RequestBody MemberEntity currentmemberEntity) {
        //定期收益接口
        String fixedtimesInfo = fixedTimeFeign.selectIdByName(currentmemberEntity.getUsername());
        return fixedtimesInfo;
    }

    @ApiOperation("活期收益")
    @PostMapping(value = "/currentcfound", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultBean getCurrentcFound (@RequestBody MemberEntity currentmemberEntity) {
        //活期收益接口
        PageInfos pageInfos = new PageInfos();
        pageInfos.setUserName(currentmemberEntity.getUsername());
        String currentcFoundInfo = currentFoundFeign.getUserCurrentInfo(pageInfos);
        MemberAccountOutBean memberAccountOutBean = JSON.parseObject(JSON.parseObject(currentcFoundInfo).getString("data"), MemberAccountOutBean.class);
        return ResultBean.builder().code(200).msg("查询成功").data(memberAccountOutBean).build();
    }

    @ApiOperation("黄金收益")
    @PostMapping(value = "/gold", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultBean getGold (@RequestBody MemberEntity currentmemberEntity) {
        //黄金收益接口
        String goldInfo = goldFeign.selectIdByName(currentmemberEntity.getUsername());
        MemberAccountOutBean memberAccountOutBean = JSON.parseObject(JSON.parseObject(goldInfo).getString("data"), MemberAccountOutBean.class);
        return ResultBean.builder().code(200).msg("查询成功").data(memberAccountOutBean).build();
    }

    @ApiOperation("基金收益")
    @PostMapping(value = "/fund", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultBean getFund (@RequestBody MemberEntity currentmemberEntity) {
        BillBean billBean = new BillBean();
        billBean.setMemberName(currentmemberEntity.getUsername());
        //基金收益接口
        String fundInfo = foundFeign.selectIdByName(billBean);
        MemberAccountOutBean memberAccountOutBean = JSON.parseObject(JSON.parseObject(fundInfo).getString("data"), MemberAccountOutBean.class);
        return ResultBean.builder().code(200).msg("查询成功").data(memberAccountOutBean).build();
    }

    @ApiOperation("总收益")
    @PostMapping(value = "/all", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultBean getAll (@RequestBody MemberEntity currentmemberEntity) {
        //定期收益
        String fixedtimesInfo = fixedTimeFeign.selectIdByName(currentmemberEntity.getUsername());
        MemberAccountOutBean memberAccountOutBeanFixed = JSON.parseObject(JSON.parseObject(fixedtimesInfo).getString("data"), MemberAccountOutBean.class);
        if(memberAccountOutBeanFixed==null){
            memberAccountOutBeanFixed = new MemberAccountOutBean();
        }
        //基金收益
        BillBean billBean = new BillBean();
        billBean.setMemberName(currentmemberEntity.getUsername());
        String fundInfo = foundFeign.selectIdByName(billBean);
        MemberAccountOutBean memberAccountOutBeanFund = JSON.parseObject(JSON.parseObject(fundInfo).getString("data"), MemberAccountOutBean.class);
        if(memberAccountOutBeanFund==null){
            memberAccountOutBeanFund = new MemberAccountOutBean();
        }
        //黄金收益
        String goldInfo = goldFeign.selectIdByName(currentmemberEntity.getUsername());
        MemberAccountOutBean memberAccountOutBeanGold = JSON.parseObject(JSON.parseObject(goldInfo).getString("data"), MemberAccountOutBean.class);
        if(memberAccountOutBeanGold==null){
            memberAccountOutBeanGold = new MemberAccountOutBean();
        }
        //活期收益
        PageInfos pageInfos = new PageInfos();
        pageInfos.setUserName(currentmemberEntity.getUsername());
        String currentcFoundInfo = currentFoundFeign.getUserCurrentInfo(pageInfos);
        MemberAccountOutBean memberAccountOutBeanCurrent = JSON.parseObject(JSON.parseObject(currentcFoundInfo).getString("data"), MemberAccountOutBean.class);
        if(memberAccountOutBeanCurrent==null){
            memberAccountOutBeanCurrent = new MemberAccountOutBean();
        }
        //计算总收益
        MemberAccountOutBean memberAccountOutBean = new MemberAccountOutBean();

        double mySumEarnings = (double) Math.round((memberAccountOutBeanCurrent.getSumEarnings() + memberAccountOutBeanFixed.getSumEarnings()
                + memberAccountOutBeanFund.getSumEarnings() + memberAccountOutBeanGold.getSumEarnings())* 100)/100;
        memberAccountOutBean.setSumEarnings(mySumEarnings);
        //计算总价值
        double mySumOverAllBalance = (double) Math.round((memberAccountOutBeanCurrent.getSumOverAllBalance() + memberAccountOutBeanFixed.getSumOverAllBalance()
                + memberAccountOutBeanFund.getSumOverAllBalance() + memberAccountOutBeanGold.getSumOverAllBalance())* 100)/100;
        memberAccountOutBean.setSumOverAllBalance(mySumOverAllBalance);
        //计算今日总收益
        double myTodaySumEarnings = (double) Math.round((memberAccountOutBeanCurrent.getTodaySumEarnings() + memberAccountOutBeanFixed.getTodaySumEarnings()
                + memberAccountOutBeanFund.getTodaySumEarnings() + memberAccountOutBeanGold.getTodaySumEarnings())* 100)/100;
        memberAccountOutBean.setTodaySumEarnings(myTodaySumEarnings);

        return ResultBean.builder().code(200).msg("查询成功").data(memberAccountOutBean).build();
    }

}
